#include "stdafx.h"
#include "game/gamemgr.h"
#include "game/statmgr.h"
#include "./bulletobj.h"

DEF_MODEL_NAME(BulletObj, "bullet")

#define BULLET_NAME "bullet"
#define BULLET_SPEED 100

#define BULLET_RANGE 0.5

BulletObj::BulletObj(int id, Color color, ImpactObj* pShootObj)
: ModelObj<BulletObj>()
, ImpactObj(id, false)
, m_radian(0.)
{
	ImpactObj::SetAdditionFlitObj(pShootObj);

	m_model.GetAnimationHelper()->ChooseModel(BULLET_NAME);
	m_model.GetAnimationHelper()->SetColor(color);
}

BulletObj::~BulletObj()
{
}

void BulletObj::OnRender(float differTime)
{
	if (!m_bVisible)
		return;

	m_model.GetAnimationHelper()->MoveOffset(m_radian, BULLET_SPEED * differTime, &m_pos);
	m_model.Render();

	if (!CheckImpacted())
	{
		CheckRange();
	}
}

void BulletObj::SetBulletStart(Point startPos, float startRadian)
{
	m_radian = startRadian;
	memcpy(&m_pos, &startPos, sizeof(Point));

	m_model.GetAnimationHelper()->SetAngle(m_radian);
	m_model.GetAnimationHelper()->MoveToPos(m_pos);
}

float BulletObj::GetRange()
{
	return BULLET_RANGE;
}

float BulletObj::GetHeadingRadian()
{
	return m_radian;
}

Point BulletObj::GetPos()
{
	return m_pos;
}

void BulletObj::CheckRange()
{
	Size size = GameMgr::GetInstance().GetStageSize();

	if (m_pos.x < 0 || m_pos.y < 0 || m_pos.x > size.cx || m_pos.y > size.cy)
	{
		m_bVisible = false;
	}
}

bool BulletObj::CheckImpacted()
{
	ImpactObj* pImpactedObj = ImpactObj::CalcIntersection();
	if (!pImpactedObj)
	{
		return false;
	}

	StatMgr::GetInstance().Impacted(GetID(), pImpactedObj->GetID());
	pImpactedObj->Impacted();
	m_bVisible = false;
	return true;
}